package user;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import objects.UserItem;

import connect.MyConnect;

public class UserControl {
	private MyConnect mc;
	private Connection con;
	public UserControl(){
		mc = new MyConnect();
		this.con = getConnect();
	}
	public Connection getConnect(){
		return mc.getConnect();
	}
	
	
	//lay tona bo uer
	//phuong thuc lay toan bo user
		public ArrayList getUserItems(){
			ArrayList list = new ArrayList();
			UserItem item = null;
			try {
				PreparedStatement pre = this.con.prepareStatement("SELECT * FROM tbluser");
				ResultSet rs = pre.executeQuery();
				if(rs!=null){
					while(rs.next()){
						item = new UserItem();
						item.setUser_id(rs.getInt("user_id"));
						item.setUser_name(rs.getString("user_name"));
						item.setUser_pass(rs.getString("user_pass"));
						item.setUser_fullname(rs.getString("user_fullname"));
						item.setUser_email(rs.getString("user_email"));
						item.setUser_job(rs.getString("user_job"));
						item.setUser_roles(rs.getString("user_roles"));
						
						list.add(item);
					}
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
			
			return list;
		}
		

		//lay user theo id
		public UserItem getUserItem(int id){
			UserItem item = null;
			String sql=" SELECT * FROM tbluser WHERE user_id = "+id;
			try {
				PreparedStatement pre = this.con.prepareStatement(sql);
				ResultSet rs = pre.executeQuery();
				if(rs!=null){
					while(rs.next()){
						item = new UserItem();
						item.setUser_id(rs.getInt("user_id"));
						item.setUser_name(rs.getString("user_name"));
						item.setUser_pass(rs.getString("user_pass"));
						item.setUser_fullname(rs.getString("user_fullname"));
						item.setUser_email(rs.getString("user_email"));
						item.setUser_job(rs.getString("user_job"));
						item.setUser_roles(rs.getString("user_roles"));
					}
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
			return item;
		}
		
		//lay user theo username , userpass
		public UserItem getUser(String usname,String uspass){
			UserItem item = null;
			String sql =" SELECT * FROM tbluser WHERE user_name = '"+usname+"' AND user_pass = '"+uspass+"' ";
			try {
				PreparedStatement pre = this.con.prepareStatement(sql);
				ResultSet rs = pre.executeQuery();
				if(rs!=null){
					System.out.println("null");
					while(rs.next()){
						System.out.println("123");
						item = new UserItem();
						item.setUser_id(rs.getInt("user_id"));
						item.setUser_name(rs.getString("user_name"));
						item.setUser_pass(rs.getString("user_pass"));
						item.setUser_fullname(rs.getString("user_fullname"));
						item.setUser_email(rs.getString("user_email"));
						item.setUser_job(rs.getString("user_job"));
						item.setUser_roles(rs.getString("user_roles"));
					}
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
			return item;
		}
		
		public boolean checkUser(String usname){
			String sql =" SELECT * FROM tbluser WHERE user_name = '"+usname+"' ";
			try {
				PreparedStatement pre = this.con.prepareStatement(sql);
				ResultSet rs = pre.executeQuery();
				if(rs.next()){
					return true;
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
			return false;
		}
		
		public static void main(String args[]){
			UserControl uc = new UserControl();
			UserItem u = uc.getUser("admin", "123456");
			if(u != null){
				System.out.print("khac null");
			}else{
				System.out.print("null");
			}
			if(uc.checkUser("admin")){
				System.out.print("co ");
			}else{
				System.out.print("khong ");
			}
		}
}
